<template>
  <div
    class="content"
    @click.stop>
    <el-button
      v-for="(item,index) in btns"
      :key="index"
      type="text"
      :data-oauthUuid="item.oauthUuid?item.oauthUuid:null"
      @click.stop="btnClick(index)">
      {{ item.label }}
    </el-button>
    <el-dropdown
      v-if="dropdownOptions.length"
      trigger="click"
      placement="bottom"
      @command="handleCommand">
      <span class="el-dropdown-link">
        更多<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item
          v-for="(item,index) in dropdownOptions"
          :key="index"
          :data-oauthUuid="item.oauthUuid?item.oauthUuid:null"
          :command="index">
          {{ item.label }}
        </el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>
  </div>
</template>
<script>
  export default {
    name: 'DropDown',
    props: {
      options: {
        type: Array,
        default: () => [],
        required: true
      },
      params: {
        type: Object,
        default: () => ({}),
        required: true
      }
    },
    data() {
      return {
        dropdownOptions: [],
        btns: []
      }
    },
    watch: {
      'options': {
        immediate: true,
        handler(val) {
          if (val.length > 2) {
            this.dropdownOptions = val.slice(1)
            this.btns = val.slice(0, 1)
          } else {
            this.dropdownOptions = []
            this.btns = val
          }
        }
      }
    },
    methods: {
      handleCommand(index) {
        if (this.dropdownOptions[index].click && this.dropdownOptions[index].click instanceof Function) {
          this.dropdownOptions[index].click(this.params)
        }
      },
      btnClick(index) {
        if (this.btns[index].click && this.btns[index].click instanceof Function) {
          this.btns[index].click(this.params)
        }
      }
    }
  }
</script>
<style scoped lang="scss">
.content{
  display: flex;
  justify-content: space-around;
}
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
    font-size: 12px;
  }
  .el-button--text{
    top: -3px;
    position: relative;
  }
</style>
